﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.Win32;
using System.Web.Security;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using System.Data;

namespace CMS_GYM
{

       
        //──────────────────────────────────────────────────────────//──────────────────────────────
     
        public class hsTool
        {
            public static string ProductName;
            //────────────────────────────────────────────────────────────────────────────────────────────
           
           
            //────────────────────────────────────────────────────────────────────────────────────────────
            public static string GetStrFileName(string FileName)
            {
                int Pos = FileName.LastIndexOf("\\");
                return FileName.Substring(0, Pos);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static bool IsNumber(string S)
            {
                if (S == null || S == "") return false;
                char[] C = S.ToCharArray();

                int i = 0; bool temp = true;
                if (C[0] == '-') i = 1;
                while (i < S.Length && temp)
                {
                    if (!char.IsNumber(C[i]) && !C[i].ToString().Equals("\b")) 
                        temp = false;
                    else i++;
                }
                return temp;
            }//end void
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static bool isDateType(string S)
            {
                try
                {
                    DateTime.Parse(S);
                    return true;
                }
                catch (Exception) {
                    return false;
                }
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static string FormatSNumberToPrint(string ins_sNumber)
            {
                string temp = ins_sNumber;
                if (temp != null)
                {
                    string[] temp2 = temp.Split('.');
                    temp = temp2[0];
                }
                if (temp.Length > 3)
                {
                    temp = temp.Insert(temp.Length - 3, ".");
                    if (temp.Length > 7)
                    {
                        temp = temp.Insert(temp.Length - 7, ".");
                        if (temp.Length > 11)
                            temp = temp.Insert(temp.Length - 11, ".");
                    }
                }
                return temp;
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static string PadText(String csText, int nLen, int nType)
            {
                if (csText.Length == nLen)
                    return csText;

                if (csText.Length > nLen)
                {
                    if (nType == 1)
                        csText = csText.Substring(csText.Length - nLen, nLen);
                    else
                        csText = csText.Substring(0, nLen);
                }
                else
                {
                    while (csText.Length < nLen)
                    {
                        if (nType == 0)
                        {
                            csText = csText + " ";
                            if (csText.Length < nLen)
                                csText = " " + csText;
                        }
                        else
                            if (nType == 1)
                                csText = " " + csText;
                            else
                                csText = csText + " ";
                    }
                }
                return csText;
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            //public static string ConvertMoneyToText(string csMoney)
            //{
            //    string csResult = "";
            //    if (csMoney == "") return "";
            //    while (csMoney.Substring(0, 1) == "0")
            //    {
            //        if (csMoney.Length > 1)
            //            csMoney = csMoney.Substring(1, csMoney.Length - 1);
            //        else
            //            if (csMoney == "0")
            //            {
            //                csMoney = "";
            //                break;
            //            }
            //    }

            //    if (csMoney != "")
            //    {
            //        string csTram = "";
            //        string csNghin = "";
            //        string csTrieu = "";
            //        string csPart1 = "";
            //        string csPart2 = "";
            //        string delimStr = ",.";
            //        char[] delimiter = delimStr.ToCharArray();
            //        string[] split = null;
            //        split = csMoney.Split(delimiter, 2);
            //        csPart1 = split[0];
            //        if (split.Length > 1)
            //        {
            //            csPart2 = split[1];
            //            if (csPart2.Length > 3)
            //                csPart2 = csPart2.Substring(0, 3);

            //            csPart2 = SplipNumber(csPart2);

            //            if (csPart2 != "")
            //                csResult = "phay " + csPart2;
            //        }

            //        if (csPart1.Length > 9)
            //        {
            //            //  MessageBox.Show("Tien thanh toan len den tien ty la dieu khong the\r\nHay xem du lieu va tinh toan lai di Oai vo van");
            //            return "";
            //        }

            //        while (csPart1.Length > 3)
            //        {
            //            if (csTram == "")
            //                csTram = csPart1.Substring(csPart1.Length - 3, 3);
            //            else
            //                csNghin = csPart1.Substring(csPart1.Length - 3, 3);

            //            csPart1 = csPart1.Substring(0, csPart1.Length - 3);

            //        }

            //        if (csTram == "")
            //            csTram = csPart1;
            //        else
            //            if (csNghin == "")
            //                csNghin = csPart1;
            //            else
            //                csTrieu = csPart1;

            //        if (csTram != "")
            //            csTram = SplipNumber(csTram);
            //        if (csNghin != "")
            //            csNghin = SplipNumber(csNghin);
            //        if (csTrieu != "")
            //            csTrieu = SplipNumber(csTrieu);

            //        if (csTram != "")
            //            csResult = csTram + csResult;
            //        if (csNghin != "")
            //            csResult = csNghin + "nghìn " + csResult;
            //        if (csTrieu != "")
            //            csResult = csTrieu + "triệu " + csResult;

            //        csResult += "đồng";
            //    }
            //    return csResult;
            //}
            public static string ConvertMoneyToText(string csMoney)
            {
                string csResult = "";
                if (csMoney == "") return "";
                while (csMoney.Substring(0, 1) == "0")
                {
                    if (csMoney.Length > 1)
                        csMoney = csMoney.Substring(1, csMoney.Length - 1);
                    else
                        if (csMoney == "0")
                        {
                            csMoney = "";
                            break;
                        }
                }

                if (csMoney != "")
                {
                    string csTram = "";
                    string csNghin = "";
                    string csTrieu = "";
                    string csTy = "";
                    string csPart1 = "";
                    string csPart2 = "";
                    string delimStr = ",.";
                    char[] delimiter = delimStr.ToCharArray();
                    string[] split = null;
                    split = csMoney.Split(delimiter, 2);
                    csPart1 = split[0];
                    if (split.Length > 1)
                    {
                        csPart2 = split[1];
                        if (csPart2.Length > 3)
                            csPart2 = csPart2.Substring(0, 3);

                        csPart2 = SplipNumber(csPart2);

                        if (csPart2 != "")
                            csResult = "phay " + csPart2;
                    }

                    //if (csPart1.Length > 9)
                    //{
                    //    //  MessageBox.Show("Tien thanh toan len den tien ty la dieu khong the\r\nHay xem du lieu va tinh toan lai di Oai vo van");
                    //    return "";
                    //}

                    while (csPart1.Length > 3)
                    {
                        if (csTram == "")
                            csTram = csPart1.Substring(csPart1.Length - 3, 3);
                        else
                        {
                            if (csNghin == "")
                                csNghin = csPart1.Substring(csPart1.Length - 3, 3);
                            else
                                csTrieu = csPart1.Substring(csPart1.Length - 3, 3);
                        }
                        csPart1 = csPart1.Substring(0, csPart1.Length - 3);

                    }

                    if (csTram == "")
                        csTram = csPart1;
                    else
                        if (csNghin == "")
                            csNghin = csPart1;
                        else
                        {
                            if (csTrieu == "")
                                csTrieu = csPart1;
                            else
                                csTy= csPart1;
                        }

                    if (csTram != "")
                        csTram = SplipNumber(csTram);
                    if (csNghin != "")
                        csNghin = SplipNumber(csNghin);
                    if (csTrieu != "")
                        csTrieu = SplipNumber(csTrieu);
                    if (csTy != "")
                        csTy = SplipNumber(csTy);

                    if (csTram != "")
                        csResult = csTram + csResult;
                    if (csNghin != "")
                        csResult = csNghin + "nghìn " + csResult;
                    if (csTrieu != "")
                        csResult = csTrieu + "triệu " + csResult;
                    if (csTy != "")
                        csResult = csTy + "tỷ " + csResult;

                    csResult += "đồng";
                }
                return csResult;
            }

            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static string SplipNumber(string csNumber)
            {
                string csText = "";
                string csResult = "";

                if (csNumber == "000" || csNumber == "00" || csNumber == "0")
                    return csResult;

                char[] NumberArr = null;
                int nCount = 0;
                NumberArr = csNumber.ToCharArray();
                for (int i = NumberArr.Length - 1; i >= 0; i--)
                {
                    switch (NumberArr[i])
                    {
                        case '0':
                            if (i == NumberArr.Length - 1)
                                csText = "";
                            else
                                if (i == NumberArr.Length - 2)
                                    if (NumberArr[NumberArr.Length - 1] != '0')
                                        csText = "lẻ ";
                                    else
                                        csText = "";
                                else
                                    csText = "không ";
                            break;
                        case '1':
                            if (i == NumberArr.Length - 2)
                                csText = "mười ";
                            else
                                csText = "một ";
                            break;
                        case '2':
                            csText = "hai ";
                            break;
                        case '3':
                            csText = "ba ";
                            break;
                        case '4':
                            csText = "bốn ";
                            break;
                        case '5':
                            if ((i == NumberArr.Length - 1) && (NumberArr.Length != 1))
                                csText = "lăm ";
                            else
                                csText = "năm ";
                            break;
                        case '6':
                            csText = "sáu ";
                            break;
                        case '7':
                            csText = "bảy ";
                            break;
                        case '8':
                            csText = "tám ";
                            break;
                        case '9':
                            csText = "chín ";
                            break;
                    }
                    if ((nCount == 1) && (NumberArr[i] != '0') && (NumberArr[i] != '1'))
                        csText += "mươi ";
                    else
                        if (nCount == 2)
                            csText += "trăm ";
                    csResult = csText + csResult;
                    nCount++;
                }
                return csResult;
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static string s_getSaveDate(string s_Date)
            {
                if (s_Date == null || s_Date == "") return "";
                try
                {
                    string s = DateTime.Parse(s_Date).ToString("yyyy-MM-dd HH:mm:ss");
                    return s;
                }
                catch (Exception) { return ""; }
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static string s_getshortDate(string s_Date)
            {
                if (s_Date == null || s_Date == "") return "";
                try
                {
                    string s = DateTime.Parse(s_Date).ToString("dd-MM-yyyy");
                    return s;
                }
                catch (Exception) { return ""; }
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static string s_getFullDate(string s_Date)
            {
                if (s_Date == null || s_Date == "") return "";
                try
                {
                    string s = DateTime.Parse(s_Date).ToString("dd-MM-yyyy HH:mm:ss");
                    return s;
                }
                catch (Exception) { return ""; }
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
				public   static string s_GetNameNotSign(string s_Value)
			{
				s_Value = s_Value.ToLower();
				char[] cut = s_Value.ToCharArray();
				string a = "";
				for (int i = 0; i <= cut.Length - 1; i++)
				{
					a = cut[i].ToString();
					switch(a)
					{
						case "á":   case "à": case "ả":case "ã":case "ạ":
						case "ă":case "ắ":    case "ằ":case "ẳ":case "ẵ":case "ặ": 
						case "â":case "ấ":    case "ầ":case "ẩ":case "ẫ":case "ậ": 
							cut[i] = 'A';break;
						case "ú": case "ù":case "ủ":case "ũ":case "ụ":
						case "ứ":case "ừ":    case "ử":case "ữ":case "ự":case "ư": 
							cut[i] = 'U';break;
						case "ó":case "ò":case "ỏ":case "õ":case "ọ":
						case "ơ":case "ớ":case "ờ":case "ở":case "ỡ":case "ợ":
						case "ô":case "ố":case "ồ":case "ổ":case "ỗ":case "ộ":
							cut[i] = 'O';break;
						case "é": case "è":case "ẻ":case "ẽ":case "ẹ":
						case "ê":case "ề":case "ế":case "ể"  : case "ễ": case "ệ":
							cut[i] = 'E';break;
						case "í":case "ì":case "ỉ":case "ĩ":case "ị":
							cut[i] = 'I';break;
						case "ý":case "ỳ":case "ỷ":case "ỹ":case "ỵ":
							cut[i] = 'Y';break;
						case "đ": cut[i] = 'D';break;
					}

				}//end for
				string susername = "";
				for (int j = 0; j < cut.Length; j++)
				{
					susername = susername + cut[j].ToString().ToLower();
				}
				return susername;
			}
			//------------------------------------------------------------------
            //───────────────────────────────────────────────────────────────────────────────────────
            public static bool Not(bool values)
            {
                if (values == false) return true;
                return false;
            }
            //───────────────────────────────────────────────────────────────────────────────────────
            public static void MakeDataGridStyle(ref DataGrid dgView, System.Data.DataTable dtLink, List<ColumnView> lstColumn)
            {
                MakeDataGridStyle(ref dgView, dtLink.DefaultView, lstColumn);
                
            }//end void
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void MakeDataGridStyle(ref DataGrid dgView, System.Data.DataView dvLink, List<ColumnView> lstColumn)
            {
                if (dvLink == null) return;

                if (lstColumn != null)
                {
                    if (dgView.TableStyles.Count == 0)
                    {
                        DataGridTableStyle dgTableStyle = new DataGridTableStyle();
                        dgTableStyle.MappingName = dvLink.Table.TableName;
                        for (int i = 0; i < lstColumn.Count; i++)
                        {
                            if (lstColumn[i].isBit)
                            {
                                DataGridBoolColumn bitdgCl = new DataGridBoolColumn();
                                bitdgCl.ReadOnly = lstColumn[i].IsReadOnly;
                                bitdgCl.NullText = "";
                                bitdgCl.Width = (int)(dgView.Width * lstColumn[i].Percent);
                                bitdgCl.Alignment = HorizontalAlignment.Left;
                                bitdgCl.MappingName = lstColumn[i].clName;
                                bitdgCl.HeaderText = lstColumn[i].clNameVN;
                                dgTableStyle.GridColumnStyles.Add(bitdgCl);

                            }
                            else
                            {

                                DataGridTextBoxColumn dgCl = new DataGridTextBoxColumn();
                                dgCl.ReadOnly = lstColumn[i].IsReadOnly;
                                if (lstColumn[i].Format != "")
                                    dgCl.Format = lstColumn[i].Format;
                                dgCl.NullText = "";
                                dgCl.Width = (int)(dgView.Width * lstColumn[i].Percent);
                                dgCl.Alignment = HorizontalAlignment.Left;
                                dgCl.MappingName = lstColumn[i].clName;
                                dgCl.HeaderText = lstColumn[i].clNameVN;
                                dgCl.NullText = "";
                                dgTableStyle.GridColumnStyles.Add(dgCl);
                            }
                        }
                        dgView.TableStyles.Add(dgTableStyle);
                        dgTableStyle.BackColor = dgView.BackColor;
                        dgTableStyle.ForeColor = dgView.ForeColor;
                        dgTableStyle.HeaderBackColor = dgView.HeaderBackColor;
                        dgTableStyle.HeaderForeColor = dgView.HeaderForeColor;
                        dgTableStyle.GridLineColor = dgView.GridLineColor;
                        dgTableStyle.SelectionBackColor = dgView.SelectionBackColor;
                        dgTableStyle.SelectionForeColor = dgView.SelectionForeColor;
                        dgTableStyle.AlternatingBackColor = dgView.AlternatingBackColor;
                        dgTableStyle.GridLineStyle = dgView.GridLineStyle;
                        dgView.BorderStyle = BorderStyle.Fixed3D;
                        dgView.FlatMode = false;
                        
                        

                    }
                    else
                    {
                        for (int j = 0; j < dgView.TableStyles[0].GridColumnStyles.Count; j++)
                        {
                            dgView.TableStyles[0].GridColumnStyles[j].Width = (int)(dgView.Width * lstColumn[j].Percent);
                        }
                    }
                }

                dgView.DataSource = dvLink;
                string s = dgView.CaptionText;
                int pos = s.IndexOf(" ,Số lượng: ");
                if (pos != -1) s = s.Remove(pos, s.Length - pos);
                s += " ,Số lượng: " + dvLink .Count.ToString();
                dgView.CaptionText = s;
                if (dvLink.Count > 0) dgView.Select(dvLink.Count - 1);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void dg_SetDataSource( ref DataGrid dgView  ,DataView dvLink)
            {
                dgView.DataSource = dvLink;
                string s = dgView.CaptionText;
                int pos = s.IndexOf(" ,Số lượng: ");
                if (pos != -1) s = s.Remove(pos, s.Length - pos);
                s += " ,Số lượng: " + dvLink .Count.ToString();
                dgView.CaptionText = s;
                if (dvLink.Count > 0) dgView.Select(dvLink.Count - 1);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            
            public static void dt_Copy(System.Data.DataTable dtResource,  int Pos,ref System.Data.DataTable dtDes)
            {
                if (dtDes == null)
                {
                    dtDes = new System.Data.DataTable();
                    System.Data.DataColumn[] arrC = new System.Data.DataColumn[dtResource.Columns.Count];
                    for (int i = 0; i < dtResource.Columns.Count; i++)
                        dtDes.Columns.Add(dtResource.Columns[i].ColumnName, dtResource.Columns[i].DataType);
                    dtDes.TableName = dtResource.TableName;
                }
                else
                    dtDes.Rows.Clear();

                System.Data.DataRow[] arrR = new System.Data.DataRow[dtResource.Rows.Count];
                dtResource.Rows.CopyTo(arrR,0);
                dtDes.ImportRow(arrR[Pos]);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void dt_MoveRow(ref DataTable dtSource, int Pos, ref DataTable dtDes)
            {
                if (dtDes == null)
                {
                    dtDes = new System.Data.DataTable();
                    System.Data.DataColumn[] arrC = new System.Data.DataColumn[dtSource.Columns.Count];
                    for (int i = 0; i < dtSource.Columns.Count; i++)
                        dtDes.Columns.Add(dtSource.Columns[i].ColumnName, dtSource.Columns[i].DataType);
                    dtDes.TableName = dtSource.TableName;
                }
                System.Data.DataRow[] arrR = new System.Data.DataRow[dtSource .Rows.Count];
                dtSource .Rows.CopyTo(arrR, 0);
                dtDes.ImportRow(arrR[Pos]);
                dtSource.Rows.RemoveAt(Pos);

            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void dt_MoveRow(ref DataTable dtSource,ref DataTable dtDes)
            {
                if (dtDes == null)
                {
                    dtDes = new System.Data.DataTable();
                    System.Data.DataColumn[] arrC = new System.Data.DataColumn[dtSource.Columns.Count];
                    for (int i = 0; i < dtSource.Columns.Count; i++)
                        dtDes.Columns.Add(dtSource.Columns[i].ColumnName, dtSource.Columns[i].DataType);
                    dtDes.TableName = dtSource.TableName;
                }
                System.Data.DataRow[] arrR = new System.Data.DataRow[dtSource.Rows.Count];
                dtSource.Rows.CopyTo(arrR, 0);
                for(int i=0;i<arrR.Length;i++)
                    dtDes.ImportRow(arrR[i]);
                dtSource.Rows.Clear();

            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void TakAutoNumber( DataTable dt)
            {
                if (dt.Columns.IndexOf("STT") == -1)
                {
                    string s="";
                    dt.Columns.Add("STT", s.GetType());
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                    dt.Rows[i]["STT"] = (i + 1).ToString(); 
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void dt_Copy(System.Data.DataTable dtResource, ref System.Data.DataTable dtDes)
            {
                if (dtDes == null)
                {
                    dtDes = new System.Data.DataTable();
                    System.Data.DataColumn[] arrC = new System.Data.DataColumn[dtResource.Columns.Count];
                    for (int i = 0; i < dtResource.Columns.Count; i++)
                        dtDes.Columns.Add(dtResource.Columns[i].ColumnName, dtResource.Columns[i].DataType);
                    dtDes.TableName = dtResource.TableName;
                }
                else
                    dtDes.Rows.Clear();

                System.Data.DataRow[] arrR = new System.Data.DataRow[dtResource.Rows.Count];
                dtResource.Rows.CopyTo(arrR, 0);
                for (int Pos = 0; Pos < dtResource.Rows.Count; Pos++)
                    dtDes.ImportRow(arrR[Pos]);
            }

            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void dgrid_SelectOne(ref   DataGrid DgView, int Pos)
            {
                System.Data.DataTable dt = dt_getDataSource(DgView);
                if (dt == null) return;
                for (int i = 0; i < dt.DefaultView .Count; i++)
                {
                    DgView.UnSelect(i);
                }
                DgView.CurrentRowIndex = Pos;
                DgView.Select(Pos);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            
            public static void CopyFile(string s_FileSource, string s_FileDes, bool Isoveride, double Percent)
            {
                System.IO.BinaryReader RCopy = null;
                System.IO.BinaryWriter WCopy = null;
                try
                {
                    RCopy = new BinaryReader(new System.IO.FileStream(s_FileSource, FileMode.Open, FileAccess.Read));

                    bool temp = false;
                    if (System.IO.File.Exists(s_FileDes))
                    {
                        if (Isoveride)
                        {
                            System.IO.File.Delete(s_FileDes);
                            WCopy = new BinaryWriter(new System.IO.FileStream(s_FileDes, FileMode.Create, FileAccess.Write));
                        }
                        else
                        {
                            WCopy = new BinaryWriter(new System.IO.FileStream(s_FileDes, FileMode.Append, FileAccess.Write));
                            temp = true;
                        }
                    }
                    else WCopy = new BinaryWriter(new System.IO.FileStream(s_FileDes, FileMode.Create, FileAccess.Write));

                    if (temp) WCopy.BaseStream.Position = WCopy.BaseStream.Length;


                    byte[] c = new byte[1];
                    int Count;
                    double pos = (double)RCopy.BaseStream.Length * Percent;
                    while ((Count = RCopy.Read(c, 0, 1)) > 0 && RCopy.BaseStream.Position < pos)
                        WCopy.Write(c[0]);
                    RCopy.Close(); WCopy.Close();

                }
                catch (Exception)
                {
                    MessageBox.Show("Không thể copy \r\n bạn nên kiểm tra lại xem nó có đang bị sử dụng bởi chương trình khác không", "Tắt nhưng chương trình khác đang truy cập file cần copy");
                    try
                    {
                        RCopy.Close();
                    }
                    catch (Exception) { }
                    try
                    {
                        WCopy.Close();
                    }
                    catch (Exception) { }
                }
            }
            public static bool b_ReadFile(string s_FileName, byte[] b_arrResult)
            {

                try
                {
                    BinaryReader RCopy = new BinaryReader(new System.IO.FileStream(s_FileName, FileMode.Open, FileAccess.Read));
                    b_arrResult = new byte[RCopy.BaseStream.Length];
                    byte[] c = new byte[1];
                    int i = 0;
                    while (RCopy.Read(c, 0, 1) > 0)
                        b_arrResult[i++] = c[0];
                    RCopy.Close();
                    return true;
                }
                catch (Exception) { return false; }

            }

            public static bool b_WriteFile(string s_FileName, byte[] arrValue)
            {
                try
                {
                    BinaryWriter WCopy = null;
                    if (!System.IO.File.Exists(s_FileName))
                        WCopy = new BinaryWriter(new System.IO.FileStream(s_FileName, FileMode.Create, FileAccess.Write));
                    else

                        WCopy = new BinaryWriter(new System.IO.FileStream(s_FileName, FileMode.Append, FileAccess.Write));

                    for (int i = 0; i < arrValue.Length; i++)
                        WCopy.Write(arrValue[i]);
                    WCopy.Close();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static string s_ReadFile(string s_FileName)
            {

                if (!System.IO.File.Exists(s_FileName))
                    return "";
                System.IO.StreamReader sR = new StreamReader(s_FileName);
                string S = sR.ReadToEnd();
                sR.Close();
                return S;

            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static bool s_WriteFile(string s_FileName, string value)
            {
                try
                {
                    if (!System.IO.File.Exists(s_FileName))
                        System.IO.File.WriteAllText(s_FileName, value);
                    else
                    {
                        System.IO.StreamWriter sW = new StreamWriter(s_FileName);
                        sW.Write(value);
                        sW.Close();
                    }
                    return true;
                }
                catch (Exception) { return false; }
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static bool isClose;
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void frm_FormClosing(object sender, FormClosingEventArgs e)
            {
                Form F = (Form)sender;
                if(F.MdiParent!=null && ! isClose)
                {
                    e.Cancel = true;
                    F.Hide();
                }
                else
                    e.Cancel = false;
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void TakeAutoNumber(ref DataGrid dgView)
            {
                DataTable dt = dt_getDataSource(dgView);
                if(dt==null) return;
                if (dt.Columns.IndexOf("STT")==-1)
                    dt.Columns.Add ("STT", dgView.CaptionText.GetType());
                for (int i = 0; i < dt.Rows.Count; i++)
                    dt.Rows[i]["STT"] = (i + 1).ToString();
                

            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            
            
            public static  DataTable dt_getDataSource(DataGrid dgView)
            {
                DataTable dt = null;
                if (dgView.DataSource == null) return null;
                if (dgView.DataSource.GetType().ToString() == "System.Data.DataView")
                {
                    DataView dvTemp = (DataView)dgView.DataSource;
                    dt = dvTemp.Table;
                }
                else     dt = (DataTable)dgView.DataSource;
                return dt;
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            
            public static bool isIPAddress(string s_IPAddress)
            {
               char[] C= s_IPAddress.ToCharArray();
               int i = 0; bool temp = true;
               while (i < C.Length && temp)
               {
                   if (!Char.IsNumber(C[i]) && C[i] != '.') temp = false;
                   else i++;
                   
               }
               return temp;
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static int int_Search(DataTable dtSearch, string s_Filter)
            {
                
                DataRow[] DR = dtSearch.Select(s_Filter );
                if (DR.Length == 0) return -1;
                return dtSearch.Rows.IndexOf(DR[0]);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static int int_Index(DataTable dt, string PriColumnValues)
            {
                try
                {

                    DataRow[] Dr = dt.Select(dt.Columns[0].ColumnName + "=" + PriColumnValues + "");
                    if (Dr.Length == 0) return -1;
                    return dt.Rows.IndexOf(Dr[0]);
                }
                catch (Exception)
                {
                    return -1;
                }

            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static int int_Index2(DataTable dt, string PriColumnValues)
            {
                if (dt == null || dt.Rows.Count == 0) return -1;
                bool ok = false;
                int i = 0;
                while (i < dt.Rows.Count && !ok)
                {
                    if (dt.Rows[i][0].ToString().Trim().ToLower() == PriColumnValues.Trim().ToLower())
                        ok = true;
                    else  i++;
                }
                if (ok) return i;
                return -1;

            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
             
            public static void lst_Copy(List<string> lst_Source, int Pos, ref List<string> lst_Des)
            {
                lst_Des.Add(lst_Source[Pos]);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void lst_Copy(ref List<string> lst_Source ,ref List<string> lst_Des)
            {
                string[] arrItem = new string[lst_Source.Count];
                lst_Source.CopyTo(arrItem, 0);
                lst_Des.AddRange(arrItem);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void lst_Cut(ref List<string> lst_Source, int Pos, ref List<string> lst_Des)
            {
                lst_Des.Add(lst_Source[Pos]);
                lst_Source.RemoveAt(Pos);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void lst_Cut(ref List<string> lst_Source, ref List<string> lst_Des)
            {
                string[] arrItem=new string[lst_Source.Count];
                lst_Source.CopyTo(arrItem,0);
                lst_Des.AddRange(arrItem);
                lst_Source.Clear();
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void dt_Remove(ref DataTable dtSource, string s_ID)
            {
              int n= int_Search(dtSource, dtSource.Columns[0].ColumnName + "='" + s_ID + "'");
              dtSource.Rows.RemoveAt(n);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void dt_Remove(ref DataTable dtSource, DataTable dtSearch)
            {
                int n=-1;
                for (int i = 0; i < dtSearch.Rows.Count; i++)
                {
                     n = int_Search(dtSource, dtSource.Columns[0].ColumnName + "='" + dtSearch.Rows[i][0].ToString() + "'");
                    dtSource.Rows.RemoveAt(n);
                }
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static void dgridSetColor(ref DataGrid dgView, int Pos, Brush brush)
            {
                dgView.CreateGraphics().FillRectangle(brush, dgView.Left + dgView.PreferredColumnWidth, dgView.Top + Pos * dgView.PreferredSize.Height, dgView.Size.Width - dgView.PreferredRowHeight, dgView.PreferredSize.Height);
            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
            
            public  static int int_NumberDayInMonth(int int_Month,int int_Year)
            {
                int n = 31;
                List<int> lst30D = new List<int>();lst30D.AddRange(new int[] {4,6,9,11});
                List<int> lst31D = new List<int>(); lst31D.AddRange(new int[] { 1, 3, 5, 7,8,10,12 });
                if (lst30D.IndexOf(int_Month) != -1) n = 30;
                else
                    if (lst31D.IndexOf(int_Month) != -1) n = 31;
                    else
                        if (int_Year % 4 == 0) n = 29;
                        else n = 28;
                return n;

            }
            //──────────────────────────────────────────────────────────────────────────────────────────     
         
            //──────────────────────────────────────────────────────────────────────────────────────────     
            public static Brush fillBrush=Brushes.Wheat ;
            public static Brush forceBrush = Brushes.Black;
            

            //──────────────────────────────────────────────────────────────────────────────────────────     

            //──────────────────────────────────────────────────────────────────────────────────────────     
            
            
            public static void  getMaxMinRow (DataView dv,int columnIndex ,ref int rowMax,ref int rowMin)
            {
                rowMax = 0; rowMin = 0;
                rowMax = 0; rowMin = 0;
                string s_valuesMax = "", s_valueMin = "";
                string values = "";
                for (int i = 0; i < dv.Count; i++)
                {
                     values = dv[i][columnIndex].ToString();
                     if (values.CompareTo(s_valuesMax)>=0)
                    {
                        rowMax = i;
                        s_valuesMax = values;
                    }
                    if (values.CompareTo(s_valueMin)<=0)
                    {
                        rowMin = i;
                        s_valueMin  = values;
                    }
                }
                
            }
            //──────────────────────────────────────────────────────────────────────────────────────────   
            public static int dg_Search(ref DataGrid dgView, string s_Value ,int  columnIndex)
            {
                DataTable dt = dt_getDataSource(dgView);
                if (dt == null || dt.Rows.Count == 0) return -1;
                DataView dv = dt.DefaultView;
                int i = 0, temp = 0;
                while (i < dv.Count && temp==0)
                {
                    if (dgView[i,columnIndex].ToString().ToLower() == s_Value.ToLower())
                        temp = 1;
                    else i++;
                }
                if (temp==1)
                {
                    dgrid_SelectOne(ref dgView, i);
                    return i;
                }
                return -1;
                  }
         //──────────────────────────────────────────────────────────────────   
            public static int int_getFirstSelectRow(DataGrid dgView)
                  {
                      DataTable dt = dt_getDataSource(dgView);
                      if (dt == null || dt.Rows.Count == 0) return -1;
                      DataView dvSource = dt.DefaultView;
                      bool temp = false; int i = 0;
                      while (i < dvSource.Count && !temp)
                      {
                          if (dgView.IsSelected(i)) temp = true;
                          else i++;
                      }
                      if (temp) return i;
                      else return -1;
                  }
            //──────────────────────────────────────────────────────────────────   
            public static int int_getFirstUnSelectRow(DataGrid dgView)
                  {
                      DataTable dt = dt_getDataSource(dgView);
                      if (dt == null || dt.Rows.Count == 0) return -1;
                      DataView dvSource = dt.DefaultView;
                      bool temp = false; int i = 0;
                      while (i < dvSource.Count && !temp)
                      {
                          if (!dgView.IsSelected(i)) temp = true;
                          else i++;
                      }
                      if (temp) return i;
                      else return -1;
                  }
             //──────────────────────────────────────────────────────────────────               
  public static string FilterPicture()
            {
                return "JPG files (*.jpg|*.jpg| BMP files (*.bmp|*.bmp| ICO files (*.ICO|*.ICO|"
                            + "png Files(*.png|*.png| Gif files(*.gif|*gif| All files (*.*)|*.* ";
            }
            //──────────────────────────────────────────────────────────────────               
            public static string FilterPictureAndVideo()
            {
                return "JPG files (*.jpg|*.jpg| BMP files (*.bmp|*.bmp| ICO files (*.ICO|*.ICO|"
               + " PNG file(*.png|*.png| gif files(*.gif|*.gif|"
               + "Media Files(*.wmv|*.wmv| mp3 Files(*.mp3|*.mp3|"
               + "Video Files(*.mp4|*.mp4| Video Clip (*.dat|*.dat|  "
               + "AVI files(*.avi|*avi| Movies file(*.movi|*.movi ";
            }
            //──────────────────────────────────────────────────────────────────               
            public static string ConvertSaveData(string value)
            {
                if (value == null || value.Trim() == "" || value=="NULL")
                    return "NULL";
                return value;
            }
            //──────────────────────────────────────────────────────────────────               
            public static string ConvertSaveStringData(string value, bool isVN)
            {
                if (value == null || value.Trim() == "" || value=="NULL") return "NULL";
                if (isVN) return "N'" + value + "'";
                else return  value;
            }
            //──────────────────────────────────────────────────────────────────    
              public static bool getCheck(string value)
            {
                if (value == null) return false;
                if (value.Trim() == "") return false;
                if (value.ToLower() == "true" || value=="1") return true;
                return false;

            }
            //──────────────────────────────────────────────────────────────────              
            public static DateTime getDatefromString(string value)
            {
                if (value == null) return DateTime.Today;
                if (value.Trim() == "") return DateTime.Today;
                try
                {
                    return DateTime.Parse(value);
                }
                catch (Exception)
                {
                    return DateTime.Today;
                }
                //──────────────────────────────────────────────────────────────────  
            }
            public static void Swap( ref  DataTable dt, int pos, int n,   int   m)
            {
                object temp = dt.Rows[n][pos];
                object temp1 = dt.Rows[n][0];
                for (int i = n; i > m; i--)
                {
                    dt.Rows[i][pos] = dt.Rows[i - 1][pos];
                    dt.Rows[i][0] = dt.Rows[i - 1][0];

                }
                dt.Rows[m][pos] = temp;
                dt.Rows[m][0] = temp1;
            }
            //──────────────────────────────────────────────────────────────────  
            public static int ConvertToInt(string value)
            {
                try
                {
                    if (value == null || value.Trim() == "" || value == "NULL") return -1;
                    return Convert.ToInt32(value);
                }
                catch (Exception) { return -1; }
            }
            public static long ConvertToLong(string value)
            {
                if (value == null || value.Trim() == "" || value == "NULL") return -1;
                return Convert.ToInt64(value);
            }
        }//end class





    }//end name space


